home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / progjrn / pj_7_6.arc / WINDEV.ARC / WINAUXNT.C < prev    next >
C/C++ Source or Header  |  1989-04-23  |  7KB  |  258 lines

  1. /* 
  2.  * Winaux initialization module
  3.  *
  4.  * Written by Bill Hall
  5.  * 3665  Benton Street, #66
  6.  * Santa Clara, CA 95051
  7.  *
  8.  */
  9.  
  10. #define NOCOMM
  11. #define NOMINMAX
  12. #define NOKANJI
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <fcntl.h>
  16. #include <io.h>
  17. #include <sys\types.h>
  18. #include <sys\stat.h>
  19. #include <windows.h>
  20. #include <ttycls.h>
  21. #include "winaux.h"
  22.  
  23. /* local function declarations */
  24. static BOOL NEAR RegisterWindowClass(HANDLE);
  25. static void NEAR getcrect(HANDLE hInstance);
  26. static BOOL NEAR MakeAndShowMainWnd(HANDLE, int);
  27. static BOOL NEAR SearchKey(char *str, char *key, int len);
  28.  
  29. /* initial window size rectangle */
  30. static RECT crect;
  31.  
  32. /* enter here to initialize the program */
  33. BOOL FAR InitProgram(hInstance,hPrevInstance, lpszCmdLine, cmdShow)
  34. HANDLE hInstance, hPrevInstance;
  35. LPSTR lpszCmdLine;
  36. int cmdShow;
  37. {
  38.  
  39.     char szMessage[80];
  40.     char crlf[3];
  41.  
  42.     crlf[0] = '\r';
  43.     crlf[1] = '\n';
  44.     crlf[2] = 0;
  45.  
  46.   /* if first instance, do these things */
  47.     if (hPrevInstance == NULL) {
  48.         LoadString(hInstance,IDS_ICONSTRING,(LPSTR)szIconTitle,
  49.                 sizeof(szIconTitle));
  50.     LoadString(hInstance,IDS_HWND,(LPSTR)szhWnd,sizeof(szhWnd));
  51.     strcpy(szBeginLog, crlf);
  52.     LoadString(hInstance,IDS_BEGINLOG,szBeginLog + 2,
  53.                     sizeof(szBeginLog) - 5);
  54.     strcat(szBeginLog, crlf);
  55.     strcpy(szEndLog, crlf);
  56.     LoadString(hInstance,IDS_ENDLOG,szEndLog + 2,
  57.                     sizeof(szEndLog) - 5);
  58.     strcat(szEndLog, crlf);
  59.     if (!RegisterWindowClass(hInstance))
  60.         return FALSE;
  61.     }
  62.     else
  63.       return FALSE;
  64.   /* no additional instances allowed */
  65.  
  66.   /* read the rectangle for create window */
  67.     getcrect(hInstance);
  68.  
  69.   /* create the window */
  70.     if (!MakeAndShowMainWnd(hInstance,cmdShow))
  71.     return FALSE;
  72.  
  73.   /* write the handle to win.ini */
  74.     if (!SetWinIni(MWnd.hWnd))
  75.     return FALSE;
  76.  
  77.     LoadString(hInstance,IDS_LOGFILE,(LPSTR)szMessage, sizeof(szMessage));
  78.     hFile = open(szMessage, O_CREAT | O_TRUNC | O_WRONLY, S_IWRITE);
  79.     if (hFile)
  80.     EnableMenuItem(GetMenu(MWnd.hWnd), IDM_LOG, MF_ENABLED);
  81.     else {
  82.     LoadString(hInstance, IDS_NOLOGFILE, szMessage, sizeof(szMessage));
  83.     MessageBox(MWnd.hWnd,szMessage,szAppName,MB_ICONEXCLAMATION | MB_OK);
  84.     }
  85.  
  86.   /* show success */
  87.     return TRUE;
  88. }
  89.  
  90. /* register the windows class */
  91. static BOOL near RegisterWindowClass(hInstance)
  92. HANDLE hInstance;
  93. {
  94.  
  95.     PWNDCLASS pWndClass;
  96.     HANDLE hTemp;
  97.  
  98.   /* get the application name from the resources */
  99.     LoadString(hInstance,IDS_APPNAME,(LPSTR)szAppName,sizeof(szAppName));
  100.  
  101.   /* make space for the WNDCLASS structure in the heap */
  102.     hTemp = LocalAlloc(LPTR,sizeof(WNDCLASS));
  103.     pWndClass = (PWNDCLASS)LocalLock(hTemp);
  104.     
  105.   /* enter data */
  106.     pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);    /* stock cursor */
  107.     pWndClass->hIcon = NULL;                /* no icon */
  108.     pWndClass->lpszMenuName = (LPSTR)szAppName;        /* this menu */
  109.     pWndClass->lpszClassName = (LPSTR)szAppName;    /* class name */
  110.     pWndClass->hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); /* background */
  111.     pWndClass->hInstance = hInstance;            /* instance */
  112.     pWndClass->style = CS_VREDRAW | CS_HREDRAW;        /* style */
  113.     pWndClass->lpfnWndProc = MainWndProc;    /* pointer to wind proc */
  114.     pWndClass->cbWndExtra = sizeof(PTTYWND);   /* space for ptr to MWnd */
  115.  
  116.   /* if failure, return FALSE */
  117.     if (!RegisterClass((LPWNDCLASS)pWndClass))
  118.     return FALSE;
  119.  
  120.   /* success, so free up heap data and show ok */
  121.     LocalUnlock(hTemp);
  122.     LocalFree(hTemp);
  123.     return TRUE;
  124. }
  125.  
  126. /* get the user's window size or load and write default values */
  127. static void NEAR getcrect(HANDLE hInstance)
  128. {
  129.  
  130.     char buf[20];
  131.     char defstr[20];
  132.     char keystr[20];
  133.     char valstr[80];
  134.     int initlen;
  135.  
  136.     initlen = GetProfileString(szAppName, NULL, "", valstr, sizeof(valstr));
  137.  
  138.     crect.left = CW_USEDEFAULT;
  139.     itoa(CW_USEDEFAULT, defstr, 10);
  140.     LoadString(hInstance,IDS_X,(LPSTR)keystr,sizeof(keystr));
  141.     if (!SearchKey(valstr, keystr, initlen))
  142.         WriteProfileString(szAppName, keystr, defstr);
  143.     GetProfileString(szAppName, keystr, defstr, buf, sizeof(buf));
  144.     crect.left = atoi(buf);
  145.  
  146.     crect.top = 0;
  147.     itoa(0, defstr, 10);
  148.     LoadString(hInstance,IDS_Y,(LPSTR)keystr,sizeof(keystr));
  149.     if (!SearchKey(valstr, keystr, initlen))
  150.         WriteProfileString(szAppName, keystr, defstr);
  151.     GetProfileString(szAppName, keystr, defstr, buf, sizeof(buf));
  152.     crect.top = atoi(buf);
  153.  
  154.     crect.right = CW_USEDEFAULT;
  155.     itoa(CW_USEDEFAULT, defstr, 10);
  156.     LoadString(hInstance,IDS_CX,(LPSTR)keystr,sizeof(keystr));
  157.     if (!SearchKey(valstr, keystr, initlen))
  158.         WriteProfileString(szAppName, keystr, defstr);
  159.     GetProfileString(szAppName, keystr, defstr, buf, sizeof(buf));
  160.     crect.right = atoi(buf);
  161.  
  162.     crect.bottom = 0;
  163.     itoa(0, defstr, 10);
  164.     LoadString(hInstance,IDS_CY,(LPSTR)keystr,sizeof(keystr));
  165.     if (!SearchKey(valstr, keystr, initlen))
  166.         WriteProfileString(szAppName, keystr, defstr);
  167.     GetProfileString(szAppName, keystr, defstr, buf, sizeof(buf));
  168.     crect.bottom = atoi(buf);
  169. }
  170.  
  171. /* create a window of the class registered above */
  172. static BOOL NEAR MakeAndShowMainWnd(hInstance, cmdShow)
  173. HANDLE hInstance;
  174. int cmdShow;
  175. {
  176.  
  177.     char szWinTitle[30];
  178.  
  179.   /* get the text for the title */
  180.     LoadString(hInstance, IDS_WINTITLE, (LPSTR)szWinTitle,sizeof(szWinTitle));
  181.  
  182.   /* create the window */
  183.     MWnd.hWnd = CreateWindow((LPSTR)szAppName,    /* class */
  184.                  (LPSTR)szWinTitle,    /* title string */
  185.                  WS_OVERLAPPEDWINDOW,    /* style */
  186.                  crect.left,   /* values obtained from getcrect */
  187.                  crect.top,
  188.                  crect.right,
  189.                  crect.bottom,
  190.                  (HWND)NULL,    /* no parent */
  191.                  (HMENU)NULL,    /* class menu */
  192.                  (HANDLE)hInstance,    /* instance */
  193.                  (LPSTR)&MWnd);    /* ptr to window structure */
  194.  
  195.   /* if successful, display the window */
  196.     if (MWnd.hWnd && MWnd.hVidBuf) {
  197.     ShowWindow(MWnd.hWnd, cmdShow);
  198.         UpdateWindow(MWnd.hWnd);
  199.     return TRUE;
  200.     }
  201.   /* return failure */
  202.     return FALSE;
  203. }
  204.  
  205. /* this routine is called upon receipt of the WM_CREATE message */
  206. void WndCreate(hWnd, lParam)
  207. HWND hWnd;
  208. LONG lParam;
  209. {
  210.  
  211.     HDC hDC;
  212.     TEXTMETRIC TM;
  213.     short width, height, cwidth, cheight;
  214.     LPCREATESTRUCT pCS;
  215.     PTTYWND pMWnd;
  216.  
  217.   /* we are going to create a buffer for a window this large */
  218.     width = GetSystemMetrics(SM_CXFULLSCREEN);
  219.     height = GetSystemMetrics(SM_CYFULLSCREEN);
  220.  
  221.   /* get the font size */
  222.     hDC = GetDC(hWnd);
  223.     GetTextMetrics(hDC, &TM);
  224.     cwidth = TM.tmAveCharWidth;
  225.     cheight = TM.tmHeight + TM.tmExternalLeading;
  226.     ReleaseDC(hWnd, hDC);
  227.  
  228.   /* here we retrieve the pointer to our local window stucture */
  229.     pCS = (LPCREATESTRUCT)lParam;
  230.     pMWnd = (PTTYWND)LOWORD(pCS->lpCreateParams);
  231.  
  232.   /* initialize the extra data to the pointer to the TWnd structure */
  233.     SetWindowWord(hWnd,0,(WORD)pMWnd);
  234.  
  235.   /* now create the text buffer and set some defaults */
  236.     InitTTYWindow(pMWnd,0,0,width,height,cwidth,cheight,FALSE,TRUE,TRUE,
  237.                     0,0x7f);
  238. }
  239.  
  240. static BOOL NEAR SearchKey(char *str, char *key, int len)
  241. {
  242.     int i;
  243.     char *startptr;
  244.     startptr = str;
  245.  
  246.     for (i = 0; i < len; i++) {
  247.     if (*str++)
  248.         ;
  249.     else {
  250.         if (strcmp(key, startptr) == 0)
  251.         return(TRUE);
  252.         startptr = str;
  253.     }
  254.     }
  255.     return (FALSE);
  256. }
  257.  
  258.